package com.ayuku.commons.ui.users;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Shell;

import com.ayuku.commons.FormDialogBasic;
import com.ayuku.commons.Util;
import com.ayuku.commons.business.user.UserImpl;
import com.ayuku.retail.persistence.model.AppUsers;

/**
 * 
 * @author putu kusuma <putukn@gmail.com>
 *
 */
public class FormUser extends FormDialogBasic {
	private CompUser cmpUser;
	private String fUserName;
	private String fNama;
	private String fPassword;
	private String fEmail;
	private String fLevel;
	private AppUsers usr;

	public FormUser(final Shell parent, final int style, final int mode) {
		super(parent, style, mode);
	}

	public void getPanelContent(final Group grpData) {
		cmpUser = new CompUser(grpData, SWT.NONE);
	}

	public void additionalProses() {
		getDialogShell().setText("Add/Edit User");
		getDialogShell().setSize(388, 230);
		try {
			Util.populateCmbGroup(cmpUser.cmbGroup);
		} catch (Exception e) {
			e.printStackTrace();
		}
		Util.setCenter(getDialogShell());
	}

	public void clearContols() {
		cmpUser.txtUsername.setText("");
		cmpUser.txtPassword.setText("");
		cmpUser.txtNamaLengkap.setText("");
		cmpUser.txtEmail.setText("");
		cmpUser.cmbGroup.setText("");
	}

	public void doLoad(String key) throws Exception {
		usr = UserImpl.getById(key);
		cmpUser.txtUsername.setText(usr.getUname());
		cmpUser.txtNamaLengkap.setText(usr.getName());
		cmpUser.txtPassword.setText("");
		cmpUser.txtEmail.setText(usr.getEmail());
		cmpUser.cmbGroup.setText(usr.getGroupId());
	}

	public boolean doValidate() {
		fUserName = cmpUser.txtUsername.getText();
		if (Util.isEmpty(fUserName)) {
			Util.showMsgBox("Username harus diisi!", 1, getDialogShell());
			cmpUser.txtUsername.setFocus();
			return false;
		}

		fNama = cmpUser.txtNamaLengkap.getText();
		if (Util.isEmpty(fNama)) {
			Util.showMsgBox("Nama harus diisi!", 1, getDialogShell());
			cmpUser.txtNamaLengkap.setFocus();
			return false;
		}

		fPassword = cmpUser.txtPassword.getText();
		if (Util.isEmpty(fPassword)) {
			Util.showMsgBox("password harus diisi!", 1, getDialogShell());
			cmpUser.txtPassword.setFocus();
			return false;
		}

		fEmail = cmpUser.txtEmail.getText();

		fLevel = cmpUser.cmbGroup.getText();
		if (Util.isEmpty(fLevel)) {
			Util.showMsgBox("Group harus diisi!", 1, getDialogShell());
			cmpUser.cmbGroup.setFocus();
			return false;
		}

		return true;
	}

	public void enabledControl(final boolean otherField, final boolean keyField) {
		cmpUser.txtUsername.setEditable(keyField);
		cmpUser.txtPassword.setEditable(otherField);
		cmpUser.txtNamaLengkap.setEditable(otherField);
		cmpUser.txtEmail.setEditable(otherField);
		cmpUser.cmbGroup.setEnabled(otherField);
	}

	public int doMainInsert() throws Exception {
		usr = new AppUsers();
		setPojoWithData();
		return UserImpl.add(usr);
	}

	public int doMainDelete() throws Exception {
		if ("administrator".equals(usr.getUname().trim())) {
			return 0;
		}
		return UserImpl.delete(usr.getUname());
	}

	public int doMainUpdate() throws Exception {
		setPojoWithData();
		return UserImpl.update(usr);

	}

	private void setModifier(AppUsers pjo) throws Exception {
		pjo.setModifiedBy(Util.getCurrentUser().getUname());
		if (pjo.getCreatedBy() == null) {
			pjo.setCreatedBy(Util.getCurrentUser().getUname());
		}
		/*
		 * pjo.setModifiedDate(Util.getCurrentDate()); if (pjo.getCreatedDate()
		 * == null) { pjo.setCreatedDate(Util.getCurrentDate()); }
		 */
	}

	@Override
	protected void setPojoWithData() throws Exception {
		usr.setUname(fUserName);
		usr.setUpass(fPassword);
		usr.setName(fNama);
		usr.setEmail(fEmail);
		usr.setGroupId(fLevel);
		setModifier(usr);
	}

	@Override
	protected void disabledOtherButton(boolean enable) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void doLoadAdd(String key) {
		// TODO Auto-generated method stub

	}

}
